package com.miplus.ccrm.report.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.miplus.ccrm.common.*;
import com.miplus.ccrm.report.controller.vo.board.RankReqVo;
import com.miplus.ccrm.report.controller.vo.board.RankResVo;
import com.miplus.ccrm.report.controller.vo.cti.CallResvo;
import com.miplus.ccrm.report.mapper.BoardMapper;
import com.miplus.ccrm.report.security.JCommonData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * 3-3.数据看板接口
 */
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/board")
public class BoardController {
    @Resource
    private BoardMapper boardMapper;

    @Resource
    private JCommonData jCommonData;

    /**
     * 催员或管理者看板
     */
    @GetMapping("/getCaseBoarding")
    public R<RankResVo> getCaseBoarding(){
        try {
            JSysUser user = jCommonData.getCurrUsr();
            String usrid = user.getUsrid();
            String grps = user.getGroups();
            String mgrps = boardMapper.getUserMgrp(user.getUsrid());
            if (JPubUtils.IsEmpty(mgrps)) {
                mgrps = grps;
            } else usrid = null;
            String[] arrs = mgrps.split(",");
            log.info("getCaseBoarding:mgrps={},usrid={}", arrs, usrid);
            return R.Success(boardMapper.getCaseBoarding(arrs, usrid));
        }
        catch (Exception ex){
            log.warn("SQLErr",ex);
            return R.Success(new RankResVo());
        }
    }

    /**
     * 催员回款排行
     */
    @GetMapping("/quyUsrRanking")
    public R<PageData<RankResVo>> quyUsrRanking(@Validated RankReqVo rankReqVo){
        try {
            String viewgrps=getCurrMgrFormat();
            String[] arrs=viewgrps.split(",");
            log.info("quyUsrRanking:{}",arrs);
            return R.SuccessPage(boardMapper.quyUsrRanking(
                    new Page<>(rankReqVo.getPageNum(), rankReqVo.getPageSize()), arrs
                    )
            );
        }
        catch (Exception ex){
            log.warn("SQLErr",ex);
            return R.Error(EnumRCode.ArgErr);
        }
    }

    private String getCurrMgrFormat()
    {
        JSysUser user= jCommonData.getCurrUsr();
        String grps=user.getGroups();
        String mgrps=boardMapper.getUserMgrp(user.getUsrid());
        if(JPubUtils.IsEmpty(mgrps)) mgrps=grps;
        return mgrps;
    }
}
